//
//  LayerLabel.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/14.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "LayerLabel.h"

@implementation LayerLabel

+(Class)layerClass
{
    NSLog(@"执行了LayerClass");
    return [CATextLayer class]; // 如果返回的不是CALayer的话，drawRect和drawLayerInContext是不会执行的
}

 - (CATextLayer *)textLayer
{
    return (CATextLayer *)self.layer;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    self.text = self.text;
    self.textColor = self.textColor;
    self.font = self.font;
    self.backgroundColor = self.backgroundColor;
    
    [self textLayer].alignmentMode = kCAAlignmentJustified;
    [self textLayer].wrapped = YES;
 //   [self.layer display];
}

- (void)setText:(NSString *)text
{
    NSLog(@"setText");
  //  super.text = text;
    [self textLayer].string = text;
}

- (void)setTextColor:(UIColor *)textColor
{
    NSLog(@"setColor");
    super.textColor = textColor;
    [self textLayer].foregroundColor = textColor.CGColor;
}

- (void)setFont:(UIFont *)font
{
    NSLog(@"setFont");
    super.font = font;
    CFStringRef fontName = (__bridge CFStringRef)(font.fontName);
    CGFontRef fontRef = CGFontCreateWithFontName(fontName);
    [self textLayer].font = fontRef;
    [self textLayer].fontSize = font.pointSize;
    CGFontRelease(fontRef);
}

- (void)setBackgroundColor:(UIColor *)backgroundColor
{
    super.backgroundColor = [UIColor clearColor];
    [self textLayer].backgroundColor = backgroundColor.CGColor;
}

- (void)drawRect:(CGRect)rect
{
    NSLog(@"drawRect"); // 不会执行
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    NSLog(@"drawLayerInContext"); // 不会执行
}




@end
